
import numpy as np
from scipy import signal
import matplotlib.pyplot as plt


fs = 48000
cutoff = 8000
width = 0.04 * 8000
ripple = 40

numtaps, beta = signal.kaiserord(ripple, width/(0.5*fs))

taps = signal.firwin(numtaps, cutoff=cutoff, window=('kaiser', beta), scale=False, fs=fs)

w, h = signal.freqz(taps, worN=8000)
w *= 0.5*fs/np.pi  # Convert w to Hz.

ideal = w < cutoff  # The "ideal" frequency response.
deviation = np.abs(np.abs(h) - ideal)
deviation[(w > cutoff - 0.5*width) & (w < cutoff + 0.5*width)] = np.nan


# plt.plot(w, 20*np.log10(np.abs(deviation)))
# plt.xlim(0, 0.5*fs)
# plt.ylim(-90, -60)
# plt.grid(alpha=0.25)
# plt.axhline(-65, color='r', ls='--', alpha=0.3)
# plt.xlabel('Frequency (Hz)')
# plt.ylabel('Deviation from ideal (dB)')
# plt.title('Lowpass Filter Frequency Response')

plt.plot(w, 20*np.log10(abs(h)))
plt.xlim(0, 0.5*fs)
# plt.ylim(-90, 10)
plt.grid(alpha=0.25)
plt.axhline(-65, color='r', ls='--', alpha=0.3)
plt.xlabel('Frequency (Hz)')
plt.ylabel(' (dB)')
plt.title('Lowpass Filter Frequency Response')

plt.show()
